//
//  BottomMenuView.swift
//  QQ空间
//
//  Created by 叶竞泽 on 2017/5/8.
//  Copyright © 2017年 叶竞泽. All rights reserved.
//

import UIKit

enum BottomMenuViewItemType {
    case mood
    case photo
    case blog
}

protocol BottomMenuViewDelegate: NSObjectProtocol {
    func didClickMenuItem(withType type: BottomMenuViewItemType)
}

class BottomMenuView: UIView {
    
    // MARK: - 属性
    
    /// 父视图
    var inView: UIView
    
    /// 子控件数组
    var subButtons: [UIButton] = [UIButton]()
    
    /// 不同设备方向时视图的高度
    var heightForPortrait: CGFloat = 200
    var heightForLandscape: CGFloat = 50
    
    /// 代理
    weak var delegate: BottomMenuViewDelegate?
    
    // MARK: - 生命周期
    
    override func layoutSubviews() {
        super.layoutSubviews()
        layoutButtons()
    }
    
    /// 初始化
    ///
    /// - Parameter inView: 父视图
    init(inView: UIView) {
        self.inView = inView
        super.init(frame: CGRect.zero)
        // 根据设备方向设置frame
        changeOrientation()
        // 创建子控件按钮
        createSubButton()
        // 监听屏幕旋转
        NotificationCenter.default.addObserver(self, selector: #selector(changeOrientation), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    // MARK: - 方法
    
    /// 监听设备旋转
    @objc private func changeOrientation() {
        switch UIDevice.current.orientation {
        case .portrait: fallthrough
        case .portraitUpsideDown:
            frame = CGRect(x: 0, y: inView.bounds.height - heightForPortrait, width: inView.bounds.width, height: heightForPortrait)
        case .landscapeLeft: fallthrough
        case .landscapeRight:
            frame = CGRect(x: 0, y: inView.bounds.height - heightForLandscape, width: inView.bounds.width, height: heightForLandscape)
        default:
            break
        }
    }
    
    /// 监听按钮点击
    @objc fileprivate func buttonClick(sender: UIButton) {
        switch sender.tag {
        case 0:
            delegate?.didClickMenuItem(withType: .mood)
        case 1:
            delegate?.didClickMenuItem(withType: .photo)
        case 2:
            delegate?.didClickMenuItem(withType: .blog)
        default:
            break
        }
    }
}

// MARK: - 设置界面
extension BottomMenuView {
    /// 添加子控件按钮
    fileprivate func createSubButton() {
        for i in 0 ..< 3 {
            let button = UIButton()
            button.addTarget(self, action: #selector(buttonClick(sender:)), for: .touchUpInside)
            button.tag = i
            button.titleLabel?.text = "\(i)"
            button.setTitleColor(UIColor.white, for: .normal)
            if i == 0{
                button.setImage(UIImage(named: "tabbar_mood"), for: .normal)
            }
            if i == 1{
                button.setImage(UIImage(named: "tabbar_photo"), for: .normal)
            }
            if i == 2{
                button.setImage(UIImage(named: "tabbar_blog"), for: .normal)
            }
            subButtons.append(button)
            addSubview(button)
        }
    }
    
    /// 根据设备方向布局子控件按钮
    fileprivate func layoutButtons() {
        switch UIDevice.current.orientation {
        case .portrait: fallthrough
        case .portraitUpsideDown:
            for i in 0 ..< subButtons.count {
                let w = bounds.width
                let h = bounds.height / CGFloat(subButtons.count)
                let y = h * CGFloat(i)
                subButtons[i].frame = CGRect(x: 0, y: y, width: w, height: h)
            }
        case .landscapeLeft: fallthrough
        case .landscapeRight:
            for i in 0 ..< subButtons.count {
                let w = bounds.width / CGFloat(subButtons.count)
                let h = bounds.height
                let x = w * CGFloat(i)
                subButtons[i].frame = CGRect(x: x, y: 0, width: w, height: h)
            }
        default:
            break
        }
    }
    
}
